<template>
	<div class="jx-list" @click="navigateTo(item['_id'])">
		<div class="user">
			<div class="user-left">
				<image :src="item['user']['avatar'] || $surl('/avatar.png')" class="user-avatar" lazy-load mode='aspectFill'></image>
				<div class="userinfo">
					<text class="uv-line-1">{{ item['user']['nickname'] }}</text>
					<!-- <text>{{item['school']['name']}}</text> -->
				</div>
			</div>
			<text class="time">{{ item['shortDate'] }}</text>
		</div>
		<div class="content">
			<view class="title">{{ item['title'] }}</view>
			<div class="body"> {{ item['body'] }} </div>
			<div class="images" v-if="item['cover']['length']">
				<image :src='item' v-for="(item, index) in item['cover']" :key="index" @click.stop="previewImage(index)" mode="aspectFill" class="image-item"
					lazy-load />
			</div>
		</div>
		<div class="community">
			<text class="menu">{{ item['menu']['name'] }}</text>
			<div class="community-right">
				<div class="community-item">
					<text>{{ item['pv'] }}</text>
					<text>围观</text>
				</div>
				<div class="community-item">
					<text>{{ item['comment_count'] }}</text>
					<text>互动</text>
				</div>
				<div class="community-item" :class="{ active: likeData['isLike'] }" @click.stop="clickLike">
					<text>{{ likeData['like_count'] }}</text>
					<i class="icon-dianzan1 iconfont" v-if="likeData['isLike']"></i>
					<i class="icon-dianzan iconfont" v-else></i>
				</div>
			</div>
		</div>
		<div class="comment" v-if="item['commentList']['length']">
			<div class="comment-item" v-for="iteml in item['commentList']" :key="iteml['_id']">
				<div class="item-name">{{ iteml['user']['nickname'] }}:</div>
				<div class="item-body">{{ iteml['content'] }}</div>
			</div>
			<div class="comment-more" v-if="item['comment_count'] > 2">
				<text>查看更多评论>></text>
			</div>
		</div>
	</div>
</template>

<script setup>
	import { ref, watch } from 'vue';
	import tools from '@/utils/tools/index.js'
	import article_likes from '@/api/article_likes/index.js'

	const props = defineProps({
		item: {
			type: Object,
			default: () => ({})
		}
	})

	const likeData = ref({ like_count: 0, isLike: false })

	function previewImage(index) {
		uni.previewImage({
			current: index,
			urls: props.item.cover
		})
	}

	function navigateTo(id) {
		uni.navigateTo({
			url: '/pages/sub/main/detail/index?id=' + id
		})
	}

	async function clickLike() {
		tools.loading();
		const { _id: article_id } = props.item;
		const response = await (likeData.value.isLike ? article_likes.delete({ article_id }) : article_likes.save({ article_id }));
		uni.hideLoading();
		if (response['code'] === 200) {
			tools.toast({ 'title': '成功' })
			likeData.value.isLike = !likeData.value.isLike;
			likeData.value.like_count += likeData.value.isLike ? 1 : -1;
		}
	}

	watch(
		() => [props.item.like_count, props.item.isLike],
		([like_count, isLike]) => {
			likeData.value = { like_count, isLike }
		}, {
			immediate: true
		}
	)
</script>

<style lang="scss" scoped>
	.jx-list {
		display: flex;
		flex-direction: column;

		.user {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.user-left {
				display: flex;

				.user-avatar {
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
				}

				.userinfo {
					display: flex;
					flex-direction: column;
					// justify-content: space-between;
					justify-content: center;
					height: 70rpx;
					margin-left: 14rpx;

					text:first-child {
						max-width: 320rpx;
					}

					// text:last-child {
					// 	font-size: 24rpx;
					// 	color: $uv-tips-color;
					// }
				}
			}

			.time {
				color: $uv-tips-color;
				font-size: 28rpx;
			}
		}

		.content {
			margin-top: 18rpx;

			.title {
				font-size: 32rpx;
				font-weight: 600;
				line-height: 44rpx;
			}

			.body {
				box-sizing: border-box;
				line-height: 46rpx;
				margin-top: 10rpx;
				width: 100%;
			}

			.images {
				display: flex;
				flex-wrap: wrap;

				.image-item {
					width: 208rpx;
					height: 208rpx;
					border-radius: 3px;
					margin-top: 14rpx;
					margin-right: 14rpx;

					&:nth-child(3n) {
						margin-right: 0;
					}
				}
			}
		}

		.community {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: $side-width;

			.menu {
				color: $uv-tips-color;
			}

			.community-right {
				display: flex;
				align-items: center;

				.community-item {
					display: flex;
					align-items: center;
					margin-left: 32rpx;

					text {
						font-size: 28rpx;
						color: $uv-tips-color;

						&:first-child {
							padding-right: 4rpx;
							font-size: 32rpx;
							color: $uv-content-color;
						}
					}

					.iconfont {
						color: $uv-tips-color;
						font-size: 34rpx;
					}

					&.active {

						text,
						.iconfont {
							color: $uv-error;
						}
					}
				}
			}
		}

		.comment {
			background: $uv-bg-color;
			padding: 20rpx;
			border-radius: 5px;
			margin-top: $side-width;

			.comment-item {
				margin-bottom: $side-width;

				&:last-child {
					margin-bottom: 0;
				}

				.item-name {
					color: $uv-primary-dark;
					font-size: 28rpx;
					margin-bottom: 10rpx;
				}

				.item-body {
					line-height: 36rpx;
					word-break: break-all;
				}
			}

			.comment-more {
				color: $uv-primary-dark;
				font-size: 28rpx;
				text-align: center;
				width: 100%;
			}
		}
	}
</style>